package com.easy.dn.ui.main.activity;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.easy.common.base.BaseActivity;
import com.easy.common.baserx.RxSubscriber;
import com.easy.common.commonwidget.DnToolbar;
import com.easy.common.commonwidget.RippleView;
import com.easy.common.commonwidget.dialog.CustomDialog;
import com.easy.dn.api.Api;
import com.easy.dn.api.ApiService;
import com.easy.dn.bean.SeachRecordsBean;
import com.easy.dn.utils.DnUtils;
import com.easy.dn.utils.MapHelper;
import com.google.android.flexbox.FlexboxLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import dn.easy.com.easyto.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * ░░░░░░░░░░░░░░░░░░░░░░░░▄░░
 * ░░░░░░░░░▐█░░░░░░░░░░░▄▀▒▌░
 * ░░░░░░░░▐▀▒█░░░░░░░░▄▀▒▒▒▐
 * ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
 * ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
 * ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
 * ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒
 * ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
 * ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄
 * ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒
 * ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒
 * 单身狗就这样默默地看着你，一句话也不说。
 * <p>
 * 搜索记录
 * 简单界面未使用MVP模式
 *
 * @author DN
 * @data 2017/7/27
 **/
public class SearchRecordsActivity extends BaseActivity implements RippleView.OnRippleCompleteListener {
    @Bind(R.id.toolbar)
    DnToolbar mToolbar;
    @Bind(R.id.flexbox_layout)
    FlexboxLayout mFlexboxLayout;
    @Bind(R.id.line)
    TextView mLine;
    @Bind(R.id.rv_list)
    RecyclerView mRvList;
    @Bind(R.id.tv_clear_data)
    RippleView mClearData;

    private String[] label = {"西门子设计师", "华为架构师", "谷歌", "星巴克", "嘉林设计总监", "电网工程师", "码帝科技",
            "苹果开发员", "阿里巴巴", "Java", "外教"};
    private int page = 1, pageCount = 10;

    private List<SeachRecordsBean> mList = new ArrayList<>();
    private SearcchRecordsAdapter mSearcchRecordsAdapter;
    private CustomDialog mAlertDialog;

    @Override
    public int getLayoutId() {
        return R.layout.act_search_record;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        mToolbar.setMainTitle(R.string.search_history);
//        mSwipeLayout.setOnRefreshListener(this);
//        mSwipeLayout.setColorSchemeColors(Color.rgb(147, 147, 147));
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        mSearcchRecordsAdapter = new SearcchRecordsAdapter(R.layout.item_search_records, mList);
        mSearcchRecordsAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mRvList.setAdapter(mSearcchRecordsAdapter);
        mClearData.setOnRippleCompleteListener(this);
        initData();
        mSearcchRecordsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                showLongToast(position+"点击了");
            }
        });

    }

    private void initData() {
        Map<String, String> map = MapHelper.getAccessMap();
        map.put("pageNum", "1");
        map.put("rowCountOfOnePage", "1000");
        map.put("type", "2");
        Api.getInstance()
                .getProxy(ApiService.class)
                .getSearchRecords(Api.getCacheControl(), map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<List<SeachRecordsBean>>(this, true) {
                    @Override
                    protected void _onNext(List<SeachRecordsBean> data) {
                        mList.clear();
                        mList = data;
                        if (mList.size() == 0) {
                            mSearcchRecordsAdapter.setEmptyView(DnUtils.getEmptyView(mContext));
                        }
                        mSearcchRecordsAdapter.setNewData(mList);
                        //动态添加热门搜索  这个东西有毒 假的？
                        for (int i = 0; i < label.length; i++) {
                            TextView textView = new TextView(mContext);
                            textView.setText(label[i]);
                            textView.setGravity(Gravity.CENTER);
                            textView.setTextSize(14);
                            textView.setBackgroundResource(R.drawable.resume_marking_select);
                            mFlexboxLayout.addView(textView);
                            ViewGroup.LayoutParams params = textView.getLayoutParams();
                            if (params instanceof FlexboxLayout.LayoutParams) {
                                FlexboxLayout.LayoutParams layoutParams = (FlexboxLayout.LayoutParams) params;
                                layoutParams.setMargins(20, 20, 0, 0);
                                textView.setLayoutParams(layoutParams);
                            }
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        DnUtils.showToast(mFlexboxLayout, message);
                    }
                });
    }
    private void showMessageNegativeDialog() {
        new QMUIDialog.MessageDialogBuilder(this)
                .setTitle("标题")
                .setMessage("确定要删除吗？")
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, "删除", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        Map<String, String> map =MapHelper.getAccessMap();
                        map.put("type", "2");
                        Api.getInstance().getProxy(ApiService.class)
                                .deleteSearchRecords(Api.getCacheControl(),map)
                                 .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new RxSubscriber<String>(mContext,true) {
                                    @Override
                                    protected void _onNext(String seachRecordsBeen) {
                                      if (seachRecordsBeen.equals("true")) {
                                          DnUtils.showToast(mFlexboxLayout,mContext.getResources().getString(R.string.delete_success));
                                          mList.clear();
                                          if (mList.size() == 0) {
                                              mSearcchRecordsAdapter.setEmptyView(DnUtils.getEmptyView(mContext));
                                          }
                                          mSearcchRecordsAdapter.notifyDataSetChanged();
                                        }

                                    }

                                    @Override
                                    protected void _onError(String message) {
                                        DnUtils.showToast(mFlexboxLayout, message);
                                    }
                                });
                        dialog.dismiss();
                    }
                })
                .show();
    }
    @Override
    public void onComplete(RippleView rippleView) {
        showMessageNegativeDialog();
//        mAlertDialog = DnUtils.showCustomDialog(mContext,"清除记录？")
//                .setOnclickListener(R.id.cancelButton, new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        mAlertDialog.dismiss();
//                    }
//                }).setOnclickListener(R.id.okButton, new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        mAlertDialog.dismiss();
//                        Map<String, String> map =MapHelper.getAccessMap();
//                        map.put("type", "2");
//                        Api.getInstance().getProxy(ApiService.class)
//                                .deleteSearchRecords(Api.getCacheControl(),map)
//                                 .subscribeOn(Schedulers.io())
//                                .observeOn(AndroidSchedulers.mainThread())
//                                .subscribe(new RxSubscriber<String>(mContext,true) {
//                                    @Override
//                                    protected void _onNext(String seachRecordsBeen) {
//                                      if (seachRecordsBeen.equals("true")) {
//                                          DnUtils.showToast(mFlexboxLayout,mContext.getResources().getString(R.string.delete_success));
//                                          mList.clear();
//                                          if (mList.size() == 0) {
//                                              mSearcchRecordsAdapter.setEmptyView(DnUtils.getEmptyView(mContext));
//                                          }
//                                          mSearcchRecordsAdapter.notifyDataSetChanged();
//                                        }
//
//                                    }
//
//                                    @Override
//                                    protected void _onError(String message) {
//                                        DnUtils.showToast(mFlexboxLayout, message);
//                                    }
//                                });
//                    }
//                })
//                .show();
    }

    public class SearcchRecordsAdapter extends BaseQuickAdapter<SeachRecordsBean, BaseViewHolder> {

        public SearcchRecordsAdapter(@LayoutRes int layoutResId, @Nullable List<SeachRecordsBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, SeachRecordsBean bean) {
            viewHolder.setText(R.id.tv_search_name, bean.getQueryCondition());
        }
    }

}
